#!/bin/bash
#
# By Steven King 11/3/2010
# kingrst@gmail.com
#
# Modified by Pierre Cazenave 3rd November 2010
# pwcazenave <at> gmail {dot} com
#
# Starts the Synergy-Plus Server
#

synergy_start() {
    
    if [ -f /var/run/synergys.pid ]; then
        echo "Synergy Server is already running"
        exit 1
    elif [ -x /usr/bin/synergys ]; then
        /usr/bin/synergys || exit $?

        pidof synergys > /var/run/synergys.pid
        echo "Synergy Server Started"
    fi

}

synergy_stop() {

    if [ -f /var/run/synergys.pid ]; then
        kill $(cat /var/run/synergys.pid) || echo "Unable to stop Synergy Server"

        rm -f /var/run/synergys.pid
        echo "Synergy Server Stopped"
    else
        echo "Synergy Server is not started"
    fi

}

synergy_status() {

    if [ -f /var/run/synergys.pid ]; then
        echo "Synergy Server Running"
        echo "PID: $(cat /var/run/synergys.pid)"
    else
        echo "Synergy Server is not started"
    fi

}


case "$1" in
    "start")
        synergy_start
        ;;
    "stop")
        synergy_stop
        ;;
    "status")
        synergy_status
        ;;
    "restart")
    	synergy_stop
	sleep 1
    	synergy_start
	;;
    *)
        echo "$0 Usage: [start|stop|restart|status]"
        ;;
esac

